//
//  VideoView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/25.
//  Copyright © 2016年 game. All rights reserved.
//

#import "VideoView.h"
#import "VideoLIstModel.h"
#import "Masonry.h"
#import "AppDelegate.h"

#define imageHight 200
#define TitleHight 40

@interface VideoView ()<UIWebViewDelegate>


@property (nonatomic,strong) UILabel *title;

@property (nonatomic,strong) UIWebView *webview;

@property (nonatomic,strong) UILabel *primCountLab;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) VideoLIstModel *videoListModel;

@property (nonatomic,strong) UIButton *zanButton;

@property (nonatomic,strong) AppDelegate *app;


@end

@implementation VideoView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
         [self CreatZanBtn];
    }
    return self;
}

-(void)setAllModel:(AticALLModel *)allModel{
    

    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.backgroundColor = [UIColor whiteColor];
    _allModel = allModel;
    
    _detaiModel = _allModel.article;
    
    _videoListModel = _allModel.video_list;
    
    NSURL *url = [NSURL URLWithString:self.videoListModel.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:request];
    
    self.title.text = [NSString stringWithFormat:@"%@\t",_detaiModel.title];
    [self.zanButton setTitle:_detaiModel.goodpost forState:UIControlStateNormal];

   
}

-(void)CreatZanBtn{
    
    
    self.zanButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-75, imageHight+2,75, TitleHight-4)];
    self.zanButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 2, 10);
    [self.zanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.zanButton setImage:[UIImage imageNamed:@"btn-praise"] forState:UIControlStateNormal];
    [self addSubview:self.zanButton];
    [self.zanButton addTarget:self action:@selector(ZanZanClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)ZanZanClick:(UIButton *)sender{

    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        if (!sender.selected) {
            sender.selected = YES;
            NSInteger count = sender.titleLabel.text.integerValue+1;
            
            [sender setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
            
            [UIView animateWithDuration:0.5 animations:^{
                
                sender.imageView.transform = CGAffineTransformScale(sender.imageView.transform, 0.65, 0.65);
            } completion:^(BOOL finished) {
                
                [self.deleget PramClick];
                
            }];
            
        }
        
    }else{
            [self.deleget PramClick];

    }


}


-(UIWebView *)webview{
    if (!_webview) {
        _webview = [UIWebView new];
        _webview.scrollView.bounces = NO;
        _webview.autoresizesSubviews = NO;
        _webview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

        _webview.contentMode = UIViewContentModeRedraw;
        _webview.opaque = YES;
        
        [self addSubview:_webview];
        
        [_webview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.equalTo(@imageHight);
        }];
    }
    return _webview;
}

-(UILabel *)title{
    if (!_title) {
        _title = [UILabel new];
        _title.font = font(SCREEN_WIDTH*0.05);
        [self addSubview:_title];
        
        UIView *view = [UIView new];
        view.backgroundColor = linColor;
        [self addSubview:view];
        
        [_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_webview).equalTo(@10);
            make.top.equalTo(_webview.mas_bottom);
            make.height.equalTo(@TitleHight);
            make.right.equalTo(self).offset(-85);
        }];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(1);
            make.width.equalTo(self);
            make.height.equalTo(@1);
        }];
    }
    return _title;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGFloat webViewHeight=[webView.scrollView contentSize].height;
    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
    webView.frame = newFrame;

}
@end